//slowing test execution

var origFn = browser.driver.controlFlow().execute;
browser.driver.controlFlow().execute = function(){
    var args = arguments;
    //queue 100 ms wait
    origFn.call(browser.driver.controlFlow(), function () {
        return protractor.promise.delayed(100);
    });
    return origFn.apply(browser.driver.controlFlow(), args);
};

var LoginPage = require('./pages/LoginPage');

describe("Login page Test", function () {
    var loginPage = new LoginPage();
    var EC = protractor.ExpectedConditions;
    it('Should be able to login', function () {
        //debugger;
        loginPage.get();

/*        browser.pause();
        browser.debugger();*/
        //console.log('title1:' + browser.getTitle()); //protractor will always return promise, console log won't log its value
        var titleGot = browser.getTitle().then(function(promisedResult){
            console.log("Title is : " + promisedResult); //value
            return promisedResult;

        });
        console.log("Title Got is " + titleGot);// promise
        expect(browser.getTitle()).toEqual('Signin Page');
        loginPage.performLogin();

        //wait browser navigate to next page
        browser.wait(EC.textToBePresentInElement(element(by.tagName('h2')), 'My Form'), 6000);
        expect(browser.getTitle()).toEqual('Submit Form');

        /*
        loginPage.setUserName(browser.params.username);
        loginPage.setPassword(browser.params.password);
        loginPage.logIn();
        */

        //console.log('title:' + browser.getTitle());
        //$('profile_info')
        //expect(browser.getTitle()).toEqual('Gentelella Alela! |');
        //expect(await element(by.css('site_title')).isPresent()).toBe(true);
       // expect(element(by.css('site_title')).isPresent()).toBe(true);
/*       element(by.css('.site_title')).isPresent().then(function(present){
           console.log("present: " + present);
            expect(present).toBe(true);
        });*/
        console.log("=======================================   completed  =======================================");
/*        element(by.linkText('index.html')).isPresent().then(function(present){
             console.log("index.html present: " + present);
             expect(present).toBe(true);
         });
         console.log("==");*/
/*        element(by.css('.site_title')).element(by.tagName('span')).isPresent().then(function(present){
            console.log("span present: " + present);
            expect(present).toBe(true);
        });*/
        //expect(element(by.linkText('index.html'))).isPresent().toBe(true);
        /*
        var spanTitle = element(by.css('.site_title')).element(by.tagName('span')).getText().then(function(promisedResult){
            console.log("Span Title is : " + promisedResult); //value
            expect(promisedResult).toEqual('Gentelella Alela!');
            return promisedResult;
        });
        expect(spanTitle).toEqual('Gentelella Alela!');
        console.log("spanTitle: " + spanTitle);
        */
/*       element(by.css('.site_title')).element(by.tagName('span')).getText().then(function(promisedResult){
            console.log("Span Title is : " + promisedResult); 
            expect(promisedResult).toEqual('Gentelella Alela!');
        });*/
    });
});
